package com.augurit.gzcgwsp.csdlwj.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.PageUtils;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.StringUtils;

import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.Page;

import com.augurit.gzcgwsp.common.util.CommonUtils;
import com.augurit.gzcgwsp.csdlwj.convert.YwSrDlwjSqbConvertor;
import com.augurit.gzcgwsp.csdlwj.dao.YwSrDlwjSqbDao;
import com.augurit.gzcgwsp.csdlwj.entity.YwSrDlwjSqb;
import com.augurit.gzcgwsp.csdlwj.service.IYwSrDlwjSqbService;
import com.augurit.gzcgwsp.csdlwj.web.form.YwSrDlwjSqbForm;
import com.augurit.gzcgwsp.hwgz.convert.YwHwShSqbConvertor;
import com.augurit.gzcgwsp.ssgd.convert.YwRqSsgdSqbConvertor;
import com.augurit.gzcgwsp.ssgd.entity.YwRqSsgdSqb;
import com.augurit.gzcgwsp.ssgd.web.form.YwRqSsgdSqbForm;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.app.doccode.web.form.OaDocCodeForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

@Service
@Transactional
public class YwSrDlwjSqbServiceImpl  extends WfBusClientServiceImpl<YwSrDlwjSqbForm> implements IYwSrDlwjSqbService{

	@Autowired
	private YwSrDlwjSqbDao ywSrDlwjSqbDao;
    @Autowired
    private IOaDocCodeService doccodeService;
    @Autowired
    private IYwComSjclService sjclService;
    @Autowired
	private IYwComSjcllistService ywComSjcllistService;
    @Autowired
    private IYwComSjclService ywComSjclService;
    @Autowired
    private IYwComQyxxService ywComQyxxService;
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public YwSrDlwjSqbForm get(Long id) {
		YwSrDlwjSqb entity = ywSrDlwjSqbDao.get(id);
		YwSrDlwjSqbForm form = YwSrDlwjSqbConvertor.convertVoToForm(entity);
		YwComQyxxForm qyxxform = ywComQyxxService.get(form.getQyid());
		YwSrDlwjSqbConvertor.convertQyxxFormToForm(qyxxform, form);
        return form;
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		ywSrDlwjSqbDao.delete(ids);
	}

	
	
	
	public void save(YwSrDlwjSqbForm... forms) {
		if(forms != null)
			for(YwSrDlwjSqbForm form : forms)
				this.save(form);
	}

	public void save(YwSrDlwjSqbForm form){
		if (form == null) return;
		
		YwSrDlwjSqb entity = null;
		if(form != null && form.getId() != null){
			entity = ywSrDlwjSqbDao.get(form.getId());
			if (StringUtils.isBlank(entity.getAcceptNo()) || form.getAcceptNo().indexOf("XXXX") > 0) form.setAcceptNo(null);
		}else{
			entity = new YwSrDlwjSqb();
            form.setAcceptNo(null);
		}
        
		//保存企业信息
        YwComQyxxForm qyxxForm = new YwComQyxxForm();
        if(form.getQyid() == null){
        	if(StringUtils.isNotBlank(form.getSqdw())){
            	YwComQyxxForm ywComQyxxForm = ywComQyxxService.getFormBySqdw(form.getSqdw(),"csdlwj");
            	if(ywComQyxxForm != null){
            		qyxxForm = ywComQyxxForm;
            	}
            }
        }
		YwSrDlwjSqbConvertor.convertFormToQyxxForm(form, qyxxForm);
		qyxxForm.setTemplateCode("csdlwj");
		ywComQyxxService.update(qyxxForm);
        form.setQyid(qyxxForm.getId());      
        
        boolean isNewForm = false;
        if (StringUtils.isBlank(form.getAcceptNo())) {
            OaDocCodeForm doccodeForm = doccodeService.get(10L);
            form.setAcceptNo(CommonUtils.convertSeqToAcceptNo(doccodeForm.getSeq()));
            doccodeService.updateForAddSeq(doccodeForm.getId());
            isNewForm = true;
        }
        YwSrDlwjSqbConvertor.convertFormToVo(form, entity);
        ywSrDlwjSqbDao.save(entity);
		form.setId(entity.getId());
		if (isNewForm) {
		    sjclService.updateByNewForm(form.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<YwSrDlwjSqbForm> search(Page<YwSrDlwjSqbForm> page, YwSrDlwjSqbForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from YwSrDlwjSqb ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		ywSrDlwjSqbDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwSrDlwjSqbForm> list = YwSrDlwjSqbConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<YwSrDlwjSqbForm> search(Page<YwSrDlwjSqbForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<YwSrDlwjSqb> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		ywSrDlwjSqbDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwSrDlwjSqbForm> list = YwSrDlwjSqbConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
    public void changeYslState(Long id,Long state) {
    	YwSrDlwjSqb entity = ywSrDlwjSqbDao.get(id);
    	entity.setYsl(state);
    	ywSrDlwjSqbDao.save(entity);
    }
    
  //是否延期办理
    public void changeIsycbl(Long id, String state) {
//    	YwRqSsgdSqbForm form = this.get(id);
//    	form.setIsycbl(state);
//    	this.save(form);
    	
    	YwSrDlwjSqb entity = ywSrDlwjSqbDao.get(id);
    	entity.setIsycbl(state);
    	ywSrDlwjSqbDao.save(entity);
		
	}

    //保存结案时间
	public void endSave(Long id) {
//		YwRqSsgdSqbForm form = this.get(id);
//		form.setJarq(new Date());
//		this.save(form);
		
		YwSrDlwjSqb entity = ywSrDlwjSqbDao.get(id);
    	entity.setJarq(new Date());
    	ywSrDlwjSqbDao.save(entity);
	}
	
	 public List<YwSrDlwjSqb> getByAcceptNo(String acceptNo)
	 {
		 String sql = "from YwSrDlwjSqb t where t.acceptNo='" + acceptNo + "'";
	        List<YwSrDlwjSqb> entityList = ywSrDlwjSqbDao.find(sql);
	        return entityList;
	 }
}
